home *** CD-ROM | disk | FTP | other *** search
/ Java Primer Plus / Java Primer Plus (Waite Group Proess)(1996).iso / java_Win / demo / BouncingHeads / BounceItem.class (.txt) < prev    next >
Encoding:
Java Class File  |  1995-12-06  |  3.3 KB  |  129 lines

  1. import java.applet.Applet;
  2. import java.applet.AudioClip;
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Dimension;
  6. import java.awt.Graphics;
  7. import java.awt.Image;
  8.  
  9. public class BounceItem extends Applet implements Runnable {
  10.    boolean images_initialized = false;
  11.    BounceImage[] images;
  12.    boolean time_to_die;
  13.    AudioClip music;
  14.    AudioClip[] sounds;
  15.    Image[] bounceimages;
  16.  
  17.    void makeImages(int var1) {
  18.       this.bounceimages = new Image[8];
  19.  
  20.       for(int var2 = 1; var2 <= 8; ++var2) {
  21.          this.bounceimages[var2 - 1] = ((Applet)this).getImage(((Applet)this).getCodeBase(), "images/jon/T" + var2 + ".gif");
  22.       }
  23.  
  24.       this.images = new BounceImage[var1];
  25.  
  26.       for(int var3 = 0; var3 < var1; ++var3) {
  27.          BounceImage var4 = this.images[var3] = new BounceImage(this);
  28.          var4.move(1.0F + (float)var4.width * 0.8F * (float)(var3 % 3) + (float)(var3 / 3) * 0.3F * (float)var4.width, (float)var4.height * 0.3F + (float)(var3 % 3) * 0.3F * (float)var4.height);
  29.       }
  30.  
  31.       this.sounds = new AudioClip[4];
  32.       this.sounds[0] = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), "audio/ooh.au");
  33.       this.sounds[1] = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), "audio/ah.au");
  34.       this.sounds[2] = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), "audio/dah.au");
  35.       this.sounds[3] = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), "audio/gong.au");
  36.       this.music = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), "audio/spacemusic.au");
  37.    }
  38.  
  39.    public void run() {
  40.       try {
  41.          if (this.images == null) {
  42.             System.out.println("Making images ...");
  43.             this.makeImages(4);
  44.          }
  45.  
  46.          if (this.music != null) {
  47.             this.music.loop();
  48.          }
  49.  
  50.          long var1 = System.currentTimeMillis();
  51.  
  52.          while(!this.time_to_die) {
  53.             long var6 = System.currentTimeMillis();
  54.             long var8 = var6 - var1;
  55.             boolean var10 = false;
  56.             Dimension var11 = ((Component)this).size();
  57.  
  58.             for(int var5 = 0; var5 < this.images.length; ++var5) {
  59.                BounceImage var12 = this.images[var5];
  60.                var12.step(var8);
  61.                if ((double)var12.Vy > 0.05 || (double)(-var12.Vy) > 0.05 || var12.y + (float)var12.width < (float)(var11.height - 10)) {
  62.                   var10 = true;
  63.                }
  64.             }
  65.  
  66.             if (!var10 && this.images.length != 0) {
  67.                for(int var17 = 0; var17 < this.images.length; ++var17) {
  68.                   BounceImage var18 = this.images[var17];
  69.                   var18.Vx = (float)Math.random() / 4.0F - 0.125F;
  70.                   var18.Vy = -((float)Math.random()) / 4.0F - 0.2F;
  71.                   var18.Vr = 0.05F - (float)Math.random() * 0.1F;
  72.                }
  73.  
  74.                if (this.sounds[3] != null) {
  75.                   this.sounds[3].play();
  76.                }
  77.             }
  78.  
  79.             ((Component)this).repaint();
  80.             var1 = var6;
  81.  
  82.             try {
  83.                Thread.sleep(100L);
  84.             } catch (InterruptedException var15) {
  85.                return;
  86.             }
  87.          }
  88.  
  89.       } finally {
  90.          if (this.music != null) {
  91.             this.music.stop();
  92.          }
  93.  
  94.       }
  95.    }
  96.  
  97.    public void init() {
  98.       Dimension var1 = ((Component)this).size();
  99.       if (var1.width <= 100 || var1.height <= 100) {
  100.          ((Applet)this).resize(500, 300);
  101.       }
  102.  
  103.    }
  104.  
  105.    public void start() {
  106.       this.time_to_die = false;
  107.       (new Thread(this)).start();
  108.    }
  109.  
  110.    public void stop() {
  111.       this.time_to_die = true;
  112.       this.music.stop();
  113.    }
  114.  
  115.    public void paint(Graphics var1) {
  116.       Dimension var2 = ((Component)this).size();
  117.       var1.setColor(Color.gray);
  118.       var1.drawRect(0, 0, var2.width - 1, var2.height - 1);
  119.       if (this.images != null) {
  120.          for(int var3 = 0; var3 < this.images.length; ++var3) {
  121.             if (this.images[var3] != null) {
  122.                this.images[var3].paint(var1);
  123.             }
  124.          }
  125.       }
  126.  
  127.    }
  128. }
  129.